home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 11526 < prev    next >
Encoding:
Text File  |  1996-08-05  |  1.4 KB  |  43 lines

  1. Path: news.ot.centuryinter.net!usenet
  2. From: steidl@centuryineter.net
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: NEWBIE: Returning 0 as refernce
  5. Date: 14 Mar 1996 23:51:15 GMT
  6. Organization: Century Internet    
  7. Message-ID: <4iabdj$r89@news.ot.centuryinter.net>
  8. References: <4huslk$rpk@badger.wmin.ac.uk> <4hvglg$v4@news4.digex.net> <4hvnbv$2rl@werple.net.au>
  9. Reply-To: steidl@centuryineter.net
  10. NNTP-Posting-Host: anx_p12.lc.centuryinter.net
  11. X-Newsreader: IBM NewsReader/2 v1.2
  12.  
  13. In <4hvnbv$2rl@werple.net.au>, davidw@werple.net.au (David White) writes:
  14. ..
  15. >I think he wants to know how to return a null reference. The answer is, 
  16. >you can't. In any case, there will be no difference in speed between 
  17.  
  18. I don't seem to have any problem returning a null reference.  Using
  19. gcc ported for OS/2 I tested the following program and it compiled
  20. without errors (or even warnings) and worked as expected.  To create
  21. a null reference dereference a null pointer, and to test if a reference is
  22. null, take its address and compare it to a null pointer.  (I don't know if
  23. this violates some C++ rule, but it works on every compiler I've used.)
  24.  
  25. -------------------------------------------------
  26.  
  27. #include <stdio.h>
  28. #include <stdlib.h>
  29.  
  30. int &foo() {
  31.    // more stuff could go here
  32.    return *((int *)0); // foo decides to return the "standard error-value"
  33. };
  34.  
  35. int main() {
  36.    int &q = foo();
  37.    if (&q == (int *)0) {
  38.       printf("Successfully got null reference\n");
  39.    };
  40.    return 0;
  41. };
  42.  
  43.